package cn.com.egova.erp.biz.impl;

import cn.com.egova.erp.biz.IReturnordersBiz;
import cn.com.egova.erp.dao.*;
import cn.com.egova.erp.entity.*;
import cn.com.egova.erp.exception.ErpException;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 退货订单业务逻辑类
 *
 * @author Administrator
 */
public class ReturnordersBiz extends BaseBiz<Returnorders> implements IReturnordersBiz {

    private IReturnordersDao returnordersDao;
    private IReturnorderdetailDao returnorderdetailDao;
    private IEmpDao empDao;
    private ISupplierDao supplierDao;
    private IStoreDao storeDao;

    public void setReturnordersDao(IReturnordersDao returnordersDao) {
        this.returnordersDao = returnordersDao;
        super.setBaseDao(this.returnordersDao);
    }

    public void setReturnorderdetailDao(IReturnorderdetailDao returnorderdetailDao) {
        this.returnorderdetailDao = returnorderdetailDao;
    }

    public void setEmpDao(IEmpDao empDao) {
        this.empDao = empDao;
    }

    public void setSupplierDao(ISupplierDao supplierDao) {
        this.supplierDao = supplierDao;
    }

    public void setStoreDao(IStoreDao storeDao) {
        this.storeDao = storeDao;
    }

    @Override
    public List<Returnorders> getListByPage(Returnorders t1, Returnorders t2, Object param, int firstResult, int maxResults) {
        List<Returnorders> list = super.getListByPage(t1, t2, param, firstResult, maxResults);
        Map<Long, String> empNameMap = new HashMap<>();
        Map<Long, String> supplierNameMap = new HashMap<>();
        for (Returnorders r : list) {
            r.setCreateName(empDao.getName(r.getCreater(), empNameMap, "getName"));
            r.setCheckName(empDao.getName(r.getChecker(), empNameMap, "getName"));
            r.setEndName(empDao.getName(r.getEnder(), empNameMap, "getName"));
            r.setSupplierName(supplierDao.getName(r.getSupplieruuid(), supplierNameMap, "getName"));
        }
        return list;
    }

    @Override
    public void add(Returnorders returnorders) {
        //设置订单状态
        returnorders.setState(Returnorders.STATE_CREATE);
        //设置下单时间
        returnorders.setCreatetime(new Date());
        double total = 0;
        for (Returnorderdetail detail : returnorders.getReturnorderdetails()) {
            //累计金额
            total += detail.getMoney();
            //订单明细状态
            detail.setState("0");
            //设置订单明细与订单的关系
            detail.setReturnorders(returnorders);
        }
        //设置累计金额
        returnorders.setTotalmoney(total);
        //保存
        returnordersDao.add(returnorders);
    }


    /**
     * 根据订单明细ID查询对应的仓库
     *
     * @param uuid
     * @return
     */
    @Override
    public Store getStoreByID(Long uuid) {
        Store store = null;
        Returnorderdetail returnorderdetail = returnorderdetailDao.get(uuid);
        Long storeuuid = returnorderdetail.getStoreuuid();
        if (storeuuid != null) {
            store = storeDao.get(storeuuid);
        }
        return store;
    }

    @Override
    public void doCheck(Long uuid, Long empUuid) {
        Returnorders returnorders = returnordersDao.get(uuid);
        if (!Returnorders.STATE_CREATE.equals(returnorders.getState())) {
            throw new ErpException("该退货订单已审核过");
        }
        returnorders.setState(Returnorders.STATE_CHECK);
        returnorders.setChecker(empUuid);
        returnorders.setChecktime(new Date());
    }

    @Override
    public void exportByTemplate(OutputStream os, Long uuid) {
        Returnorders orders = returnordersDao.get(uuid);
        List<Emp> empList = empDao.getList(null, null, null);
        Map<Long, String> empNameMap = new HashMap<>();
        for (Emp emp : empList) {
            empNameMap.put(emp.getUuid(), emp.getName());
        }
        //模板替换有点像EL表达式或OGNL表达式的用法
        Map<String, Object> dataMap = new HashMap<>();
        String type = orders.getType();
        dataMap.put("type", "1".equals(type) ? "采购退货单" : "销售退货单");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        dataMap.put("o", orders);
        dataMap.put("emp", empNameMap);
        dataMap.put("sdf", sdf);
        dataMap.put("suppliername", supplierDao.get(orders.getSupplieruuid()).getName());
        HSSFWorkbook wk = null;
        try {
            wk = new HSSFWorkbook(new ClassPathResource("export_return_orders.xls").getInputStream());
            XLSTransformer transformer = new XLSTransformer();
            transformer.transformWorkbook(wk, dataMap);
            wk.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (wk != null) {
                try {
                    wk.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
